package br.com.automed.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
@Table(name="CM_CLINICA")
@NamedQueries({
	@NamedQuery(name="listarClinica", query="FROM Clinica c")
})
public class Clinica {
	
	public static final String NAMED_QUERY_LIST_CLINICA = "listarClinica";
	
	@Id
    @GeneratedValue(generator = "system-uuid", strategy=GenerationType.AUTO)
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(name = "ID_CLINICA")
	private String idClinica;

	@Column(name = "ST_CLINICA")
	private String clinica;
	
	@Column(name = "ST_CNPJ")
	private String cnpj;
	
	@Column(name = "ST_EMAIL")
	private String email;
	
	@OneToMany(mappedBy="clinica")
	private List<Funcionario> funcionarios;

	//trazer telefones com NamedQuery
	@ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name="CM_TELEFONE_CLINICA",
    joinColumns=@JoinColumn(name="ID_CLINICA"),
    inverseJoinColumns=@JoinColumn(name="ID_TELEFONE"))
    @LazyCollection(LazyCollectionOption.FALSE)
	private List<Telefone> telefones;

	@OneToOne(cascade=CascadeType.ALL)
	@JoinColumn(name="FK_ENDERECO", referencedColumnName="ID_ENDERECO")
	private Endereco endereco;
	
	public Clinica() {
		endereco = new Endereco();
		telefones = new ArrayList<Telefone>();
	}
	
	public Clinica(String idClinica, String clinica) {
		this.idClinica = idClinica;
		this.clinica = clinica;
	}

	public String getIdClinica() {
		return idClinica;
	}

	public void setIdClinica(String idClinica) {
		this.idClinica = idClinica;
	}

	public String getClinica() {
		return clinica;
	}

	public void setClinica(String clinica) {
		this.clinica = clinica;
	}

	public String getCnpj() {
		return cnpj;
	}

	public void setCnpj(String cnpj) {
		this.cnpj = cnpj;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public List<Funcionario> getFuncionarios() {
		return funcionarios;
	}

	public void setFuncionarios(List<Funcionario> funcionarios) {
		this.funcionarios = funcionarios;
	}

	public List<Telefone> getTelefones() {
		return telefones;
	}

	public void setTelefones(List<Telefone> telefones) {
		this.telefones = telefones;
	}

	public Endereco getEndereco() {
		return endereco;
	}

	public void setEndereco(Endereco endereco) {
		this.endereco = endereco;
	}

	@Override
	public int hashCode() {
		
		return idClinica.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		
		Clinica clinica = (Clinica) obj;
		
		if(clinica.getIdClinica().equals(this.idClinica)){
			return true;
		}
		
		return false;
	}

	@Override
	public String toString() {
		
		return this.idClinica + ";" + this.clinica;
	}
}
